<template>
    <div>
         <!-- 面包屑导航区 -->
        <el-breadcrumb separator-research="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>学生管理</el-breadcrumb-item>
        <el-breadcrumb-item>学生科研</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
             <!-- 搜索 添加 -->
            <el-row :gutter="20">
                <el-col :span="10">
                    <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getResearchList">
                        <el-button type="success" slot="append" icon="el-icon-search" @click="getResearchList"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="13">
                    <el-button type="primary" @click="showAddResearchDialog" style="float:right">添加科研</el-button>
                </el-col>
            </el-row>
            <el-table :data="researchList" border stripe :header-cell-style="table_header" empty-text="暂无数据">
                <!-- 索引列 -->
                <el-table-column type="index" label="序号" width="50px" align="center"></el-table-column>
                <el-table-column label="学号" prop="stu_id" align="center" width="120px"></el-table-column>
                <el-table-column label="姓名" prop="student.name" align="center"></el-table-column>
                <el-table-column label="科研名称" prop="activity_name" align="center" width="150px"></el-table-column>
                <el-table-column label="获奖情况" prop="prize" align="center" min-width="80px"></el-table-column>
                <el-table-column label="研究方向" prop="research_direction" align="center" min-width="80px"></el-table-column>
                <el-table-column label="主办方" prop="organizers" align="center" width="120px"></el-table-column>
                <el-table-column label="科研负责人" prop="project_leader" align="center" width="100px"></el-table-column>
                <el-table-column label="参与时间" prop="part_time" align="center" width="100px">
                    <template slot-scope="scope">{{scope.row.part_time | dateFormat}}</template>
                </el-table-column>
                <el-table-column label="研究成果" prop="research_achievement" align="center" min-width="80px"></el-table-column>
                <el-table-column label="角色" prop="project_role" align="center" width="100px"></el-table-column>
                <el-table-column label="操作" width="200px" align="center" fixed="right">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditResearchDialog(scope.row)">编辑</el-button>
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeResearchById(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
        <!-- 分页区域 -->
        <div research="pager-box">
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pageNum"
                :page-sizes="[2, 5, 10, 15]"
                :page-size="queryInfo.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                prev-text="上一页"
                next-text="下一页"
                :total="total"
                background
                hide-on-single-page
            ></el-pagination>
        </div>

        <!-- 编辑科研对话框 -->
        <el-dialog :title="Dialog_title" :visible.sync="ResearchDialogVisible" width="60%" @close="resetForm('ResearchFormRef')">
            <el-form
                :model="ResearchForm"
                ref="ResearchFormRef"
                :rules="ResearchFormRules"
                label-width="100px"
                :inline="true"
            >
                <el-form-item label="学号" prop="stu_id">
                    <el-select v-model.number="ResearchForm.stu_id" filterable placeholder="请选择">
                        <el-option
                                v-for="item in stu_id_list"
                                :key="item"
                                :label="item"
                                :value="item">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="科研名称" prop="Research_name">
                <el-input v-model="ResearchForm.activity_name"></el-input>
                </el-form-item>
                <el-form-item label="研究方向" prop="Research_name">
                    <el-input v-model="ResearchForm.research_direction"></el-input>
                </el-form-item>
                <el-form-item label="研究成果" prop="Research_name">
                    <el-input v-model="ResearchForm.research_achievement"></el-input>
                </el-form-item>
                <el-form-item label="获奖情况" prop="prize">
                    <el-input v-model="ResearchForm.prize"></el-input>
                </el-form-item>
                <el-form-item label="主办方" prop="organizers">
                    <el-input v-model="ResearchForm.organizers"></el-input>
                </el-form-item>
                <el-form-item label="参与日期" prop="part_time">
                    <el-date-picker
                            v-model="ResearchForm.part_time"
                            type="date"
                            placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="项目负责人" prop="project_leader">
                    <el-input v-model="ResearchForm.project_leader"></el-input>
                </el-form-item>
                <el-form-item label="项目角色" prop="project_role">
                    <el-input v-model="ResearchForm.project_role"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="ResearchDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="AddOrUpdateResearch">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data () {
        return {
            queryInfo: {
                type: '科研',
                query: '',
                // 当前页数
                pageNum: 1,
                // 每页显示多少数据
                pageSize: 10,
            },
            total: 0,
            // 所有科研列表
            researchList: [],
            stu_id_list: [],

            Dialog_state: "",
            Dialog_title: "",

            ResearchDialogVisible: false,
            ResearchForm: {},
            ResearchFormRules: {
                stu_id: [
                    { required: true, message: '请输入学号', trigger: 'blur' }
                ],
                research_name: [
                    { required: true, message: '请输入科研名称', trigger: 'blur' }
                ],
                part_time:[
                    { required: true, message: '请选择参与时间', trigger: 'blur' }
                ],
            },
        }
    },
    created () {
        this.getResearchList();
        this.getStuIDList();
    },
    methods: {
        table_header(row, column, rowIndex, columnIndex){
            return 'background-color: rgb(102,177,255);color: white;font-size:14px;'
        },
        // 监听 pagesize改变的事件
        handleSizeChange (newSize) {
            // console.log(newSize)
            this.queryInfo.pageSize = newSize
            this.getResearchList()
        },
        // 监听 页码值 改变事件
        handleCurrentChange (newSize) {
            // console.log(newSize)
            this.queryInfo.pageNum = newSize
            this.getResearchList()
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.ResearchForm = {};
        },   
        async getResearchList() {
            const { data: res, status: status } = await this.axios.get('activity/getActivityList', {params: this.queryInfo});
            if (status !== 200) {
                return this.$message.error('获取科研列表失败！')
            }
            this.researchList = res.data.activities;
            this.queryInfo.total = res.data.total;
        },
        async getStuIDList() {
            const {data: res, status: status} = await this.axios.get('student/getStuIDList');
            if (status !== 200) {
                return this.$message.error('获取学生列表失败！')
            }
            this.stu_id_list = res.data;
        },
        showAddResearchDialog(){
            this.Dialog_state = "add";
            this.Dialog_title = "添加科研";
            this.ResearchDialogVisible = true
        },
        // 编辑科研
        async showEditResearchDialog (Research) {
            this.Dialog_state = 'edit';
            this.Dialog_title = "编辑科研";
            this.ResearchForm = Research;
            this.ResearchDialogVisible = true
        },
        // 添加科研A
        AddOrUpdateResearch () {
            var research_form = JSON.parse(JSON.stringify(this.ResearchForm));
            research_form.student = {};
            research_form.type = "科研";
            if (this.Dialog_state === "add") {
                this.$refs.ResearchFormRef.validate(async valid => {
                    if (!valid) return
                    const {data: res, status: status} = await this.axios.post('activity/addActivity', research_form);
                    if (status !== 201) {
                        this.$message.error('添加科研失败！')
                    }
                    this.$message.success('添加科研成功！');
                    this.getResearchList();
                })
            }else {
                this.$refs.ResearchFormRef.validate(async valid => {
                    if (!valid) return
                    const { data: res, status: status } = await this.axios.put('activity/updateActivity', research_form);
                    if (status !== 200) {
                        this.$message.error('更新科研信息失败！')
                    }
                    this.$message.success('更新科研信息成功！');
                    this.getResearchList();
                })
            }
            this.ResearchDialogVisible = false
        },
        // 删除科研
        async removeResearchById (Research) {
            const confirmResult = await this.$confirm(
                '此操作将永久删除科研 ' + Research.name +  ',  是否继续?',
                '提示',
                {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }
            ).catch(err => err);
            if (confirmResult !== 'confirm') {
                return this.$message.info('已取消删除')
            }
            const { data: res, status: status } = await this.axios.delete('activity/deleteActivity', {params: {"id": Research.id}});
            if (status !== 200) return this.$message.error('删除科研失败！');
            this.$message.success('删除科研成功！')
            this.getResearchList();
        },

    } 
}
</script>
<style lang="less" scoped>

</style>